﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace EnDecrypter
{
    class EncryptHelper
    {
        /// <summary>
        /// DES文件加密
        /// </summary>
        /// <param name="inFile">输入文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns></returns>
        public static bool EncryptDES(string inFile, string outFile, byte[] DESKeys, byte[] DESIVKey)
        {
            byte[] rgb = DESKeys;
            FileStream inFs = null;
            FileStream outFs = null;
            try
            {
                byte[] rgbKeys = DESIVKey;//Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                inFs = new FileStream(inFile, FileMode.Open, FileAccess.Read);//读入流
                outFs = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);// 等待写入流
                outFs.SetLength(0);//帮助读写的变量
                byte[] byteIn = new byte[100];//放临时读入的流
                long readLen = 0;//读入流的长度
                long totalLen = inFs.Length;//读入流的总长度
                int everylen = 0;//每次读入流的长度
                DES des = new DESCryptoServiceProvider();//将inFile加密后放到outFile
                CryptoStream encStream = new CryptoStream(outFs, des.CreateEncryptor(rgb, rgbKeys), CryptoStreamMode.Write);
                while (readLen < totalLen)
                {
                    everylen = inFs.Read(byteIn, 0, 100);
                    encStream.Write(byteIn, 0, everylen);
                    readLen = readLen + everylen;
                }
                encStream.Close();
                inFs.Close();
                outFs.Close();
                return true;//加密成功
            }
            catch (Exception ex)
            {
				inFs.Close();
                outFs.Close();
                MessageBox.Show(ex.Message.ToString());
                return false;//加密失败
            }
        }
        /// <summary>
        /// AES文件加密
        /// </summary>
        /// <param name="inFile">输入文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns></returns>
        public static bool EncryptAES(string inFile, string outFile, byte[] AESKeys, byte[] AESIVKey)
        {
            byte[] rgb = AESKeys;
            FileStream inFs = null;
            FileStream outFs = null;
            try
            {
                byte[] rgbKeys = AESIVKey;//Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                inFs = new FileStream(inFile, FileMode.Open, FileAccess.Read);//读入流
                outFs = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);// 等待写入流
                outFs.SetLength(0);//帮助读写的变量
                byte[] byteIn = new byte[100];//放临时读入的流
                long readLen = 0;//读入流的长度
                long totalLen = inFs.Length;//读入流的总长度
                int everylen = 0;//每次读入流的长度
                Rijndael aes = Rijndael.Create();//AES加密算法
                CryptoStream encStream = new CryptoStream(outFs, aes.CreateEncryptor(rgb, rgbKeys), CryptoStreamMode.Write);
                while (readLen < totalLen)
                {
                    everylen = inFs.Read(byteIn, 0, 100);
                    encStream.Write(byteIn, 0, everylen);
                    readLen = readLen + everylen;
                }
                encStream.Close();
                inFs.Close();
                outFs.Close();
                return true;//加密成功
            }
            catch (Exception ex)
            {
                inFs.Close();
                outFs.Close();
                MessageBox.Show(ex.Message.ToString());
                return false;//加密失败
            }
        }
        /// <summary>
        /// DES文件解密
        /// </summary>
        /// <param name="inFile">输入文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns></returns>
        public static bool DecryptDES(string inFile, string outFile, byte[] DESKeys, byte[] DESIVKey)
        {
            byte[] rgb = DESKeys;
            FileStream inFs = null;
            FileStream outFs = null;
            try
            {
                byte[] rgbKeys = DESIVKey;//Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                inFs = new FileStream(inFile, FileMode.Open, FileAccess.Read);//读入流
                outFs = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);// 等待写入流
                outFs.SetLength(0);//帮助读写的变量
                byte[] byteIn = new byte[100];//放临时读入的流
                long readLen = 0;//读入流的长度
                long totalLen = inFs.Length;//读入流的总长度
                int everylen = 0;//每次读入流的长度
                DES des = new DESCryptoServiceProvider();//将inFile加密后放到outFile
                CryptoStream encStream = new CryptoStream(outFs, des.CreateDecryptor(rgb, rgbKeys), CryptoStreamMode.Write);
                while (readLen < totalLen)
                {
                    everylen = inFs.Read(byteIn, 0, 100);
                    encStream.Write(byteIn, 0, everylen);
                    readLen = readLen + everylen;
                }
                encStream.Close();
                inFs.Close();
                outFs.Close();
                return true;//加密成功
            }
            catch (Exception ex)
            {
                inFs.Close();
                outFs.Close();
                MessageBox.Show(ex.Message.ToString());
                return false;//加密失败
            }
        }
        /// <summary>
        /// AES文件解密
        /// </summary>
        /// <param name="inFile">输入文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns></returns>
        public static bool DecryptAES(string inFile, string outFile, byte[] AESKeys, byte[] AESIVKey)
        {
            byte[] rgb = AESKeys;
            FileStream inFs = null;
            FileStream outFs = null;
            try
            {
                byte[] rgbKeys = AESIVKey;//Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                inFs = new FileStream(inFile, FileMode.Open, FileAccess.Read);//读入流
                outFs = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);// 等待写入流
                outFs.SetLength(0);//帮助读写的变量
                byte[] byteIn = new byte[100];//放临时读入的流
                long readLen = 0;//读入流的长度
                long totalLen = inFs.Length;//读入流的总长度
                int everylen = 0;//每次读入流的长度
                Rijndael aes = Rijndael.Create();//AES加密算法//将inFile加密后放到outFile
                CryptoStream encStream = new CryptoStream(outFs, aes.CreateDecryptor(rgb, rgbKeys), CryptoStreamMode.Write);
                while (readLen < totalLen)
                {
                    everylen = inFs.Read(byteIn, 0, 100);
                    encStream.Write(byteIn, 0, everylen);
                    readLen = readLen + everylen;
                }
                encStream.Close();
                inFs.Close();
                outFs.Close();
                return true;//加密成功
            }
            catch (Exception ex)
            {
                inFs.Close();
                outFs.Close();
                MessageBox.Show(ex.Message.ToString());
                return false;//加密失败
            }
        }
       
    }
}
